package com.wenwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsCultureFeatures;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsCultureFeaturesMapper;
import com.wenwu.dao.RelicsHumanityEnvironmentMapper;
import com.wenwu.service.CulturalService;

@Service
public class CulturalServiceImpl implements CulturalService {
	
	@Autowired
	private RelicsCultureFeaturesMapper relicsCultureFeaturesMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;

	public String addObj(RelicsCultureFeatures relicsCultureFeatures)
			throws RelicsException {
		int result = 0;
		try {
			List<RelicsAttachment> list = relicsCultureFeatures.getFileList();
			if (relicsCultureFeatures.getCultureId()==null) {
				result += relicsCultureFeaturesMapper.insertSelective(relicsCultureFeatures);
				if (relicsCultureFeatures.getCultureId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("文化面貌附件");
							attachment.setTableOwner(relicsCultureFeatures.getCultureId() );// 外键
						}
						result += attachmentMapper.insertList(list);
					}
				}
				if (result > 0) {
					return "文化面貌添加成功";
				} else {
					return "文化面貌添加失败";
				}
			} else {
				result += attachmentMapper
						.deleteByKey(relicsCultureFeatures.getCultureId(),relicsCultureFeatures.getOwnList());// 先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("文化面貌附件");
						attachment.setTableOwner(relicsCultureFeatures.getCultureId());// 外键
					}
					result += attachmentMapper.insertList(list);
				}
				result += relicsCultureFeaturesMapper.updateByPrimaryKeySelective(relicsCultureFeatures);
				if (result > 0) {
					return "文化面貌修改成功";
				} else {
					return "文化面貌修改失败";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RelicsException("系统错误，操作失败");
		}
	}

	public RelicsCultureFeatures selectObjByKey(Integer basicId)
			throws RelicsException {
		try {
			return relicsCultureFeaturesMapper.selectObjByKey(basicId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public RelicsCultureFeatures selectThisObjByKey(Integer cultureId)
			throws RelicsException {
		try {
			return relicsCultureFeaturesMapper.selectByPrimaryKey(cultureId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public PaginationVO selectList(RelicsCultureFeatures relicsCultureFeatures,
			PaginationVO pageVo) throws RelicsException {
		try {
			pageVo.setTotalRecord(relicsCultureFeaturesMapper.selectTotalRecord(relicsCultureFeatures));
			pageVo.setList(relicsCultureFeaturesMapper.selectList(relicsCultureFeatures, pageVo));
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
		return pageVo;
	}

	public int deleteByKey(List<?> keys) throws RelicsException {
		try {
			return relicsCultureFeaturesMapper.deleteByKey(keys);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

}
